package com.zenglx.multilanguage.util;

import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.zenglx.multilanguage.metadata.ZlxTableInfo;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * sql工具类
 * @author liangxi.zeng
 */
public class SqlHelper {
    private static final String AND = " AND ";

    /**
     * 获取所有查询列，如id,name,code...
     *
     * @param tableInfo entityClass
     * @return String
     */
    public static String getAllColumns(TableInfo tableInfo) {
        ZlxTableInfo zlxTableInfo = (ZlxTableInfo)tableInfo;
        List<TableFieldInfo> columnList = tableInfo.getFieldList();
        Set<TableFieldInfo> multiLanguageColumns = zlxTableInfo.getMultiLanguageColumns();
        List<String> multiLanguageColumnNames = multiLanguageColumns.stream().map(TableFieldInfo::getColumn).collect(Collectors.toList());
        StringBuilder sql = new StringBuilder();
        if(!multiLanguageColumns.isEmpty()) {
            sql.append("b.").append(tableInfo.getKeyColumn()).append(",");
        }
        for (TableFieldInfo entityColumn : columnList) {
            if(zlxTableInfo.isMultiLanguage()) {
                if (multiLanguageColumnNames.contains(entityColumn.getColumn())) {
                    sql.append("case when ");
                    sql.append("t.");
                    sql.append(entityColumn.getColumn());
                    sql.append(" is null ");
                    sql.append(" then b.");
                    sql.append(entityColumn.getColumn());
                    sql.append(" else t.");
                    sql.append(entityColumn.getColumn());
                    sql.append(" end as ");
                    sql.append(entityColumn.getColumn());
                    sql.append(",");
                } else {
                    sql.append("b.");
                    sql.append(entityColumn.getColumn()).append(",");
                }
            } else {
                sql.append(entityColumn.getColumn()).append(",");
            }
        }
        return sql.substring(0, sql.length() - 1);
    }


    /**
     * from tableName - 动态表名
     *
     * @param tableInfo tableInfo
     * @return String
     */
    public static String selectFromTableTl(TableInfo tableInfo) {
        StringBuilder builder = new StringBuilder();
        builder.append(tableInfo.getTableName());
        builder.append(" ");
        ZlxTableInfo zlxTableInfo = (ZlxTableInfo)tableInfo;
        if (zlxTableInfo.isMultiLanguage()) {
            builder.append("b ")
                    .append("<bind name=\"lang\" value=\"@com.zenglx.multilanguage.util.LanguageHelper@language()\"/> ")
                    .append("LEFT JOIN ")
                    .append(tableInfo.getTableName())
                    .append("_tl t ")
                    .append("ON (");
            builder.append("b.").append(tableInfo.getKeyColumn())
                    .append("=t.").append(tableInfo.getKeyColumn())
                    .append(AND);
            builder.append("t.lang=#{lang}");
            builder.append(")");
        }
        return builder.toString();
    }
}
